﻿using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRepository
{
    public interface IPurchaseOrderRepository
    {
        /// <summary>
        /// 添加进货订单主表（返回自增的purchase_id）
        /// </summary>
        /// <param name="order">进货订单实体</param>
        /// <returns>新增的进货ID（purchase_id）</returns>
        Task<int> AddPurchaseOrderAsync(PurchaseOrderEntity order, IDbTransaction transaction);

        /// <summary>
        /// 批量添加进货订单明细（需传入主表的purchase_id关联）
        /// </summary>
        /// <param name="details">明细列表</param>
        /// <returns>影响的行数</returns>
        Task<int> AddPurchaseOrderDetailsAsync(List<WmsOrderDetails> details, IDbTransaction transaction);

        /// <summary>
        /// 显示添加的进货订单
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="purchaseOrderNo"></param>
        /// <param name="status"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public Task<PagedViewModel<PurchaseOrderDTO>> GetPurchaseOrdersAsync(
            int currentPage, int pageSize,
            string? purchaseOrderNo, string? status,
            DateTime? startDate, DateTime? endDate);
        /// <summary>
        /// 开启数据库事务
        /// </summary>
        IDbTransaction BeginTransaction();

        /// <summary>
        /// 根据订单号查询主表信息（含汇总数据和供应商名称）
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        Task<PurchaseOrderMainDTO> GetMainOrderByNoAsync(string orderNo);

        /// <summary>
        /// 根据订单号查询明细列表（关联货品表）
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        Task<List<PurchaseOrderDetailDTO>> GetDetailsByNoAsync(string orderNo);

        Task UpdateReview(string code, string txt, string status, string reviewer, DateTime reviewTime);


        /// <summary>
        /// 根据订单号查询主表+明细
        /// </summary>
        Task<(PurchaseOrderMainDTO main, List<PurchaseOrderDetailDTO> details)> GetOrderWithDetailsAsync(string orderNo);

        /// <summary>
        /// 按条件查询关联进货单
        /// </summary>
        Task<PagedViewModel<PurchaseOrderMainDTO>> GetRelatedOrdersAsync(
            string inboundType, List<string> status, int? supplierId, string? orderNo,
            string? purchaser, string? department, string? date, int page, int size);

        /// <summary>
        /// 查询订单关联的货品
        /// </summary>
        Task<PagedViewModel<PurchaseOrderDetailDTO>> GetOrderGoodsAsync(
            string orderNo, string? codeOrName, string? unit, int page, int size);


        /// <summary>
        /// 按订单号查询进货订单（关联入库单的associated_order_no）
        /// </summary>
        Task<PurchaseOrderEntity2> GetByOrderNoAsync(string purchaseOrderNo, IDbTransaction transaction);

        /// <summary>
        /// 更新进货订单状态（部分入库8/已完成4）
        /// </summary>
        Task UpdateAsync(PurchaseOrderEntity2 entity, IDbTransaction transaction);

        /// <summary>
        /// 按进货ID查询进货明细
        /// </summary>
        Task<List<PurchaseOrderDetailEntity>> GetByPurchaseIdAsync(int purchaseId, IDbTransaction transaction);
    }
}
